﻿using System.IO;

namespace TanzimSaqib.NoBrainer
{
    using System.Web.UI;

    public class NoBrainerProcessor : Page
    {
        private string _Url { get; set; }
        private string _PhysicalFile { get; set; }

        protected override void CreateChildControls()
        {
            _Url = Request.QueryString[Constants.URL].Remove(0, 1); // remove fwd slash
            string.Format("NoBrainer: QueryString was null for {0}", Request.RawUrl).Null(_Url);

            _PhysicalFile = Path.Combine(Request.PhysicalApplicationPath, _Url);

            using (var reader = new StreamReader(_PhysicalFile))
            {
                var content = reader.ReadToEnd();

                Controls.Add(ParseControl(content));

                if (Form != null)
                    Form.Action = Request.RawUrl;
            }
        }
    }
}
